const db = wx.cloud.database({
  env: 'gtss-2b1812'
})
Page({
  data: {
    type: '',
    isSave: false,
    id: '',
    name: '',
    dept: '',
    phone: '',
    sex: '',
    stuClass: '',
    stuState: '',
    teaPost: '',
    teaTop: '',
    teaStu: ''
  },
  onLoad: function(options) {
    const that = this
    that.setData({
      type: options.type,
      id: options.id
    })
    if (options.type == 'stu') {
      db.collection('Student').doc(options.id).get().then(res => {
        that.setData({
          name: res.data.stuName,
          dept: res.data.stuDept,
          phone: res.data.stuPhone,
          sex: res.data.stuSex,
          stuClass: res.data.stuClass,
          stuState: res.data.stuState == 1 ? '已选题' : '未选题'
        })
      })
    }
    if (options.type == 'tea') {
      db.collection('Teacher').doc(options.id).get().then(res => {
        that.setData({
          name: res.data.teaName,
          dept: res.data.teaDept,
          phone: res.data.teaPhone,
          sex: res.data.teaSex,
          teaPost: res.data.teaPost
        })
      })
      db.collection('Topic').where({
        teaId: options.id
      }).count().then(res => {
        that.setData({
          teaTop: res.total
        })
      })
      db.collection('Topic').where({
        teaId: options.id,
        ktzt: 1
      }).count().then(res => {
        that.setData({
          teaStu: res.total
        })
      })
    }
  },
  funBj: function() {
    this.setData({
      isSave: true
    })
  },
  funSave: function() {
    const that = this
    that.setData({
      isSave: false
    })
    wx.cloud.callFunction({
      name: 'saveUserInfo',
      data: {
        id: that.data.id,
        type: that.data.type,
        name: that.data.name,
        sex: that.data.sex,
        phone: that.data.phone,
        stuClass: that.data.stuClass,
        teaPost: that.data.teaPost
      }
    }).then(res => {
      if (res.result.result == 1) {
        wx.showToast({
          title: '保存成功！',
          icon: 'success',
          duration: 2000
        })
      }
      if (res.result.result == 0) {
        wx.showToast({
          title: '保存失败！',
          icon: 'none',
          duration: 2000
        })
      }
    })
  },
  funDel: function() {
    const that = this
    wx.showModal({
      title: '提示',
      content: '确认删除？',
      cancelColor: '#000000',
      cancelText: '确认',
      confirmColor: '#576B95',
      confirmText: '取消',
      success(res) {
        if (res.confirm) {} else if (res.cancel) {
          wx.cloud.callFunction({
            name: 'deleteUserInfo',
            data: {
              type: that.data.type,
              id: that.data.id
            }
          }).then(res => {
            if (res.result.result == 1) {
              wx.showToast({
                title: '删除成功！',
                icon: 'success',
                duration: 2000
              })
              setTimeout(function() {
                wx.navigateBack({
                  delta: 1
                })
              }, 2500)
            } else {
              wx.showModal({
                title: '提示',
                content: '用户删除失败！',
                showCancel: false
              })
            }
          })
        }
      }
    })
  },
  inpName: function(e) {
    this.setData({
      name: e.detail.value
    })
  },
  inpSex: function(e) {
    this.setData({
      sex: e.detail.value
    })
  },
  inpPhone: function(e) {
    this.setData({
      phone: e.detail.value
    })
  },
  inpClass: function(e) {
    this.setData({
      stuClass: e.detail.value
    })
  },
  inpPost: function(e) {
    this.setData({
      teaPost: e.detail.value
    })
  }
})